package com.yyge.web.servlet;

import com.yyge.dto.GoodsInfoDto;
import com.yyge.service.GoodsInfoService;
import com.yyge.service.impl.GoodsInfoServiceImpl;
import com.yyge.util.ServletUtil;
import com.yyge.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/")
public class SearchGoodsInfoServlet extends HttpServlet {
    private GoodsInfoService service=new GoodsInfoServiceImpl();
    private static Logger logger= LoggerFactory.getLogger(SearchGoodsInfoServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将request请求中获取url参数，并构建为GoodsInfoDto的对象
        // GoodsInfoDto对象中包含了分页查询的条件和页码等参数
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        GoodsInfoDto goodsInfoDto= ServletUtil.parameter2dto(req,GoodsInfoDto.class);
        R r=service.selectAll(goodsInfoDto);

       // 得到pageInfo,包含了页码，pageSize,total,list
        Object pageInfo=r.getData();

        // 将pageVo保存到request作用域中
        // 再做一次页面转发，将request请求转发到另外的用于展示数据的jsp页面中
        // jsp页面只复制遍历，展示数据

        req.setAttribute("pageInfo",pageInfo);

         // 用户不能够在浏览器中访问到WEB-INF目录中的文件
        RequestDispatcher requestDispatcher=req.getRequestDispatcher("/WEB-INF/pages/goodsInfo.jsp");
        // 页面转发


        requestDispatcher.forward(req,resp);
        // 记： 页面转发是一次发生在服务器内部的行为
        // 浏览器中请求的url地址不会变化，用户不能感知到服务器在内部多个servlet之间跳转了多少次
        // 用户请求：/goods/search 这个servlet
        // 实际上用户看到的页面来自于/WEB-INF/pages/goodsInfo.jsp

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
